<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

/**
 * 提供 dec 服务的控制器。
 */
class DecController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('request.logger');
        // 关闭调试栏输出
        if (class_exists('\Debugbar')) {
            \Debugbar::disable();
        }
    }

    /**
     * 提供给DEC访问的 接口服务。
     */
    public function index(Request $request){
        try
        {
            $action = $request->action;
            $data = $request->data;
            $data = json_decode($data, TRUE );
            switch ($action)
            {
                case "data_change":
                    $this->saveDecData($data);
                    break;
                case "data_delete":
                    $this->deleteDecData($data);
                    break;
            }

            return [
                "error_code" => 0
            ];
        }
        catch(\Exception $e)
        {
            return [
                'error_code' => 1,
                'error_message'  => $e->getMessage(),
            ];
        }
    }

    public function saveDecData($data)
    {
        try
        {
            $dataArray = $data['data_array'];
            foreach ($dataArray as $d){
                $category = $d['category'];
                switch ($category)
                {
                    case "bdc_grade":
                        $gradeData = [
                            'id' => $d['id'],
                            'grade' => isset($d['curr_grade']) ? $d['curr_grade'] : '',
                            'enter_year' => isset($d['enter_year']) ? $d['enter_year'] : '',
                            'status' => isset($d['state']) ? $d['state'] : '',
                        ];
                        foreach ($gradeData as $key=>$value){
                            if(empty($value)){
                                unset($gradeData[$key]);
                            }
                        }
                        /** @var \App\Services\System\GradeService $gradeService */
                        $gradeService = app('gradeService');
                        $gradeService->updateOrCreateCore(
                            ['id' => $d['id']],
                            $gradeData
                        );
                        break;
                    case "bdc_classroom":
                        $classData = [
                            'id' => $d['id'],
                            'code' => isset($d['code']) ? $d['code'] : '',
                            'grade_id' => isset($d['grade_number']) ? $d['grade_number'] : '',
                            'grade' => isset($d['curr_grade']) ? $d['curr_grade'] : '',
                            'class_title' => isset($d['class_name']) ? $d['class_name'] : '',
                            'start_year' => isset($d['enter_year']) ? $d['enter_year'] : '',
                            'school_year' => isset($d['school_year']) ? $d['school_year'] : '',
                            'term' => isset($d['semester']) ? $d['semester'] : '',
                            'tutor' => isset($d['tutor']) ? $d['tutor'] : '',
                            'tutor2' => isset($d['tutor2']) ? $d['tutor2'] : '',
                            'teachers' => isset($d['teachers']) ? $d['teachers'] : '',
                            'students' => isset($d['students']) ? $d['students'] : '',
                            'student_number' => isset($d['student_number']) ? $d['student_number'] : '',
                            'status' => isset($d['state']) ? $d['state'] : '',
                        ];
                        foreach ($classData as $key=>$value){
                            if(empty($value)){
                                unset($classData[$key]);
                            }
                        }
                        /** @var \App\Services\System\ClassroomService $classroomService */
                        $classroomService = app('classroomService');
                        $classroomService->updateOrCreateCore(
                            ['id' => $d['id']],
                            $classData
                        );
                        $classroomCore = $classroomService->getCore($d['id']);
                        $classroomCore = $classroomCore->toArray();

                        if(isset($d['teachers'])){
                            /** @var \App\Services\System\TeacherService $teacherService */
                            $teacherService = app('teacherService');
                            $teacherService->deleteCore(['classroom_id' => $classroomCore['id']],false);
                            $teachersArr = json_decode($classroomCore['teachers'], TRUE );
                            if(!empty($teachersArr)){
                                $teacherData = [];
                                foreach ($teachersArr as $teacher){
                                    $teacherData[] = [
                                        'teacher_id' => $teacher['teacher_id'],
                                        'subject' => $teacher['subject'],
                                        'teacher_name' => $teacher['teacher_name'],
                                        'classroom_id' => $classroomCore['id'],
                                        'classroom_name' => $classroomCore['grade'].$classroomCore['class_title'],
                                        'grade_id' => $classroomCore['grade_id'],
                                        'grade_name' => $classroomCore['grade'],
                                        'created_at' => date("Y-m-d H:i:s"),
                                        'updated_at' => date("Y-m-d H:i:s"),
                                    ];
                                }
                                $teacherService->addAllCore($teacherData);
                            }
                        }

                        break;
                    case "xhjw_term_cycle":
                        $termData = [
                            'id' => $d['id'],
                            'year' => isset($d['school_year']) ? $d['school_year'] : '',
                            'termname' => isset($d['term']) ? $d['term'] : '',
                            'startdate' => isset($d['start_time']) ? $d['start_time'] : '',
                            'enddate' => isset($d['end_time']) ? $d['end_time'] : '',
                            'created_at' => date("Y-m-d H:i:s"),
                            'updated_at' => date("Y-m-d H:i:s"),
                        ];
                        foreach ($termData as $key=>$value){
                            if(empty($value)){
                                unset($termData[$key]);
                            }
                        }
                        /** @var \App\Services\System\TermService $termService */
                        $termService = app('termService');
                        $termService->updateOrCreateCore(
                            ['id' => $d['id']],
                            $termData
                        );
                        break;
                }
            }
        }
        catch(\Exception $e)
        {
            Log::error($e);
            throw $e;
        }
    }

    public function deleteDecData($data)
    {
        try
        {
            $dataArray = $data['data_array'];
            foreach ($dataArray as $d){
                $category = $d['category'];
                switch ($category)
                {
                    case "bdc_grade":
                        /** @var \App\Services\System\GradeService $gradeService */
                        $gradeService = app('gradeService');
                        $gradeService->deleteCore($d['id']);
                        break;
                    case "bdc_classroom":
                        /** @var \App\Services\System\ClassroomService $classroomService */
                        $classroomService = app('classroomService');
                        $classroomService->deleteCore($d['id']);

                        /** @var \App\Services\System\TeacherService $teacherService */
                        $teacherService = app('teacherService');
                        $teacherService->deleteCore(['classroom_id' => $d['id']],false);
                        break;
                }
            }
        }
        catch(\Exception $e)
        {
            Log::error($e);
            throw $e;
        }
    }
}
